package com.km.team.phsystem.service.impl;

import cn.hutool.core.date.DateUtil;
import com.km.team.phsystem.beans.pojo.TemporaryAppointment;
import com.km.team.phsystem.beans.vo.TimeSlotVo;
import com.km.team.phsystem.common.utils.LocalDateUtil;
import com.km.team.phsystem.service.*;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;

/**
 * @author ：Riven.
 * @description：
 * @date ：2018/12/7
 */
@Service("timeSlotAssistService")
public class TimeSlotAssistServiceImple implements TimeSlotAssistService {
    private final TimeSlotPhConfigService timeSlotPhConfigService;
    private final IQueueService queueService;
    private final TemporaryAppointmentService temporaryAppointmentService;

    @Autowired
    public TimeSlotAssistServiceImple(TimeSlotPhConfigService timeSlotPhConfigService, IQueueService queueService,
                                      TemporaryAppointmentService temporaryAppointmentService) {
        this.timeSlotPhConfigService = timeSlotPhConfigService;
        this.queueService = queueService;
        this.temporaryAppointmentService = temporaryAppointmentService;
    }

    /**
     * 获取开启的时段
     *
     * @param businessId 业务
     * @param date       日期
     * @return
     */
    @Override
    public TimeSlotVo getEnableTimeSlotListForBusinessId(Integer businessId, String date) {
        Date date1 = LocalDateUtil.stampToDate(date);
        TimeSlotVo result =
            timeSlotPhConfigService.getEnableTimeSlotListForBusinessId(businessId, date1);
        result.getTimeSlotList().forEach(item -> {
            Long aLong = this.queueService.selectQueueNumFotBusinessIdAndDateAndTimeSlot(businessId,
                date1, item.getTimeSlotId());
            if (aLong >= item.getAcceptNo()) {
                item.setAvailable(false);
                item.setEableAcceptNo(0L);
            } else {
                item.setAvailable(true);
                item.setEableAcceptNo(item.getAcceptNo() - aLong);
            }
        });
        TemporaryAppointment one = temporaryAppointmentService.selectOneForBusinessId(businessId);
        if (one.getAcceptNo() == null || one.getDoAcceptNo() == null) {
            result.setTemporary(0);
            result.setTemporaryAvailable(false);
        } else {
            result.setTemporary(one.getAcceptNo() - one.getDoAcceptNo());
            result.setTemporaryAvailable(one.getAcceptNo() > one.getDoAcceptNo());
        }
        result.setTody(true);
        return result;
    }
}
